package com.dreamstations.changshads.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.StoryBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.DSUtils;
import com.dreamstations.changshads.util.RequestType;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

/**
 * Created by 7YHong on 2016/6/20.
 */
public class Story_Simple_Frag extends BaseFragmentForSection<StoryBean> {

    public Story_Simple_Frag(){}


    @SuppressLint("ValidFragment")
    public Story_Simple_Frag(String title, @RequestType String reqType, int category){
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        bundle.putString("reqType",reqType);
        bundle.putInt("category",category);
        setArguments(bundle);
    }

    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle=getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                .addParams("page",String.valueOf(1))
                //二级分类
                .setTag(Story_Simple_Frag.this)
                .addParams("typeId",String.valueOf(bundle.getInt("category")));
    }

    Picasso picasso ;
    @NonNull
    @Override
    public DSRvAdapter.RvSupport<StoryBean> getItem() {
        picasso = Picasso.with(getContext());
        return new DSRvAdapter.RvSupport<StoryBean>(){

            @Override
            public int getItemViewType(StoryBean data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                return ViewHolder.get(context,null,parent, R.layout.story_information_item,-1);
            }

            @Override
            public void convert(ViewHolder holder, StoryBean data) {
                holder.setText(R.id.textView7,data.getTitle());
                holder.setText(R.id.item_time,data.getTime());
                if (data.getCover() == null || data.getCover().equals("")) {  //没有封面
                    return ;
                }else {
                    ImageView imageView = holder.getView(R.id.item_cover);
                    Picasso.with(getActivity())
                            .load(DSRequest.SERVER_BASE1 + data.getCover())
                            .error(R.drawable.home_nav_header)
                            .into(imageView);
                }
            }
        };
    }

    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<StoryBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, StoryBean o, int position) {
                Intent intent;
                if (o.getTemplate() == 1) {
                    intent = new Intent(getActivity(), ArticleDetail_WebActivity.class);
                    intent.putExtra("data", o);
                } else {
                    intent = new Intent(getActivity(), ArticleDetailActivity.class);
                    intent.putExtra("data", o);
                }
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, StoryBean o, int position) {
                return false;
            }
        };
    }

    @Override
    public DSRvAdapter.RvSupport getHeader() {
        return null;
    }

    @Override
    public boolean getmEnablePullLoad() {
        return true;
    }
}
